package com.example.config.securityConfig;

import com.example.dao.TUserMapper;
import com.example.model.entity.TUser;
import com.example.model.query.TUserQuery;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class SelfUserDetailsService  {

    @Resource
    private TUserMapper userMapper;

    public TUserQuery loadUserByUsername(String username)  {

        TUserQuery userInfo = new TUserQuery();
        userInfo.setUserName(username); //任意登录用户名

        TUser user = userMapper.selectUserByUserName(username);

        if (user == null || ObjectUtils.isEmpty(user.getPassword())) {
            throw new UsernameNotFoundException("User name" + username + "not find!!");
        }
        userInfo.setPassword(user.getPassword()); //从数据库获取密码

//        Set<SimpleGrantedAuthority> authoritiesSet = new HashSet<>();
//        List<String> roles = userService.findRoleNameByUsername(username);
//        for (String roleName : roles) {
//            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleName); //用户拥有的角色
//            authoritiesSet.add(simpleGrantedAuthority);
//        }
//        userInfo.setAuthorities(authoritiesSet);

        return userInfo;
    }
}
